<template>
    <div>
        <div class="form-item  form-item-style1">
            <div class="label-title">
                <label>单位名称</label>
            </div>
            <div>
                <p>{{userInfo.deptname}}</p>
            </div>
        </div>
        <div class="form-item  form-item-style1">
            <div class="label-title">
                <label>职务</label>
            </div>
            <div>
                <p>{{userInfo.usermode}}</p>
            </div>
        </div>
        <div class="form-item  form-item-style1">
            <div class="label-title">
                <label>姓名</label>
            </div>
            <div>
                <p>{{userInfo.username}}</p>
            </div>
        </div>
        <div class="form-item  form-item-style1">
            <div class="label-title">
                <label>时间</label>
            </div>
            <div>
                <p>{{resumptionDate}}</p>
            </div>
        </div>
        <h2>
            <span>工作内容（{{items.length}}条）</span>
            <span @click="recordItemPropShow = true">添加</span>
        </h2>
        <swipeout class="lists">
            <swipeout-item transition-mode="follow"
                           v-for="(item,index) in items"
                           :key="index">
                <div slot="right-menu">
                    <swipeout-button @click.native="onButtonClick(index)"
                                     type="warn">删除</swipeout-button>
                </div>
                <div slot="content">
                    <div class="item"
                         @click="openChild(item)">
                        <div>
                            <h5>{{item.description}}</h5>
                        </div>
                        <div class="status">
                            <x-icon type="ios-arrow-down"
                                    size="20"
                                    class="icon-right"
                                    v-show="!item.open"></x-icon>
                            <x-icon type="ios-arrow-up"
                                    size="20"
                                    class="icon-right"
                                    v-show="item.open"></x-icon>
                        </div>
                    </div>
                    <div class="form-section sectionML20"
                         style="border-bottom:1px solid #f4f4f4;"
                         v-show="item.open"
                         @click="modify(item)">
                        <div class="form-item form-item-style2">
                            <div class="label-title space-between">
                                <div class="space-between">
                                    <label>图片</label>
                                </div>
                                <span>当前共{{item.uploadLength}}张照片</span>
                            </div>
                            <div class="upload-wrap">
                                <div>
                                    <div class="upload-item"
                                         v-for="(item,index) in item.imgList"
                                         :key="index">
                                        <img :src="item"
                                             alt="">
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-item  form-item-style1">
                            <div class="label-title">
                                <label>时间</label>
                            </div>
                            <div>
                                <p>{{item.resumptionTime}}</p>
                            </div>
                        </div>
                    </div>
                </div>
            </swipeout-item>
        </swipeout>
        <div class="cut-off-rule"
             v-if="items.length>0"></div>
        <div class="form-item form-item-style2">
            <div class="label-title space-between">
                <div class="space-between">
                    <label>工作总结</label>
                </div>
            </div>
            <div>
                <textarea placeholder="请输入工作总结"
                          v-model="jobSummary"
                          maxlength="10"></textarea>
            </div>
        </div>
        <div class="btns">
            <button @click="addSubmit">提交</button>
        </div>
        <addRecordItem :recordItemPropShow="recordItemPropShow"
                       :formData="formData"
                       :addItemFn="addItemFn"
                       :initialData="initialData"></addRecordItem>
        <myMenu></myMenu>
    </div>
</template>
<script>
import addRecordItem from './addRecordItem'
import formatDate from '@/utils/formatDate'
import myMenu from '@/commonComponents/menu'
import upload from '@/commonComponents/upload/index'
export default {
    name: 'addRecord',
    components: {
        myMenu,
        upload,
        addRecordItem
    },
    data () {
        return {
            preview: new Date().getTime(), // 预览图片分组
            userInfo: {},
            resumptionDate: formatDate(),
            jobSummary: '',
            items: [],
            formData: new FormData(),
            initialData: '',
            recordItemPropShow: false
        }
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo'))
    },
    methods: {
        openChild (item) {
            this.$set(item, 'open', !item.open)
        },
        onButtonClick (index) {
            this.items.splice(index, 1)
        },
        addItemFn (item) {
            this.recordItemPropShow = false
            if (this.initialData) {
                for (let i = 0; i < this.items.length; i++) {
                    if (this.items[i].ID == item.ID) {
                        this.items[i] = item
                        this.initialData = ''
                        break
                    }
                }
            } else {
                this.items.unshift(item)
            }
            this.$forceUpdate()
        },
        modify (item) {
            this.recordItemPropShow = true
            this.initialData = ''
            this.$nextTick(() => {
                this.initialData = item
            })
        },
        addSubmit () {
            this.$vux.loading.show({
                text: '提交中，请稍等...'
            })
            let param = {
                'allowPaging': true,
                'business': 'InsertOrUpdateResumption',
                'data': {
                    'DeleAttachItems': [],
                    'ID': '',
                    'createDeptName': '',
                    'createUser': '',
                    'deleItems': [],
                    'duty': this.userInfo.usermode, // 职务
                    'itemCount': this.items.length, // 工作内容数量
                    'items': this.items,
                    'jobSummary': this.jobSummary,
                    'resumptionDate': this.resumptionDate, // 日期
                    'submitStatus': '1'
                },
                'pageIndex': 0,
                'pageSize': 0,
                'tokenId': 'CDE91EECCB7C8A47D32A244D7583EB5B',
                'userId': this.userInfo.userid
            }
            this.items.forEach(item => {
                for (var key of item.uploadImgFormData.keys()) {
                    this.formData.append(item.ID, item.uploadImgFormData.get(key))
                }
            })
            this.$axiosAjax.getConfigJson(
                param,
                this.formData
            ).then((res) => {
                if (res.data.code === 0) {
                    this.$router.go(-1)
                } else {
                    this.$vux.toast.text(res.data.info)
                }
                this.formData = new FormData()
            })
        }
    }
}
</script>
<style scoped>
h2 {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 40px;
    line-height: 40px;
    padding: 0 20px;
    box-sizing: border-box;
    background: #ebeef2;
}
h2 span,
h2 a {
    font-size: 14px;
    color: #333;
}
h2 span:last-child {
    background: #2467f6;
    color: #fff;
    height: 30px;
    line-height: 30px;
    padding: 0 15px;
    border-radius: 15px;
}
.lists {
    padding-left: 0;
}
.lists .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 14px 20px 14px 20px;
    border-bottom: 1px solid #efeff4;
}
.lists h5 {
    font-size: 14px;
    color: #333;
}
</style>
